/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import viviendas.Conexion;

/**
 *
 * @author x
 */
public class Registro extends Conexion {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        String irA = "login";
        try {



            String nombres = request.getParameter("nombres");
            String apellidos = request.getParameter("apellidos");
            String direccion = request.getParameter("direccion");
            String telefono = request.getParameter("telefono");
            String usuario = request.getParameter("usuario");
            String clave = request.getParameter("clave");
            
            
            ResultSet rs = executeQueryList("select usuario from usuarios where usuario like '"+ usuario +"'");
           if (rs.next()){
           irA="nuevousuario";
           setErrorMessage("El usuario ya existe");
           
           }else{
           
           
            save("insert into usuarios(id_tipo_usuario, nombres, apellidos, direccion, telefono, usuario, clave) values("
                    + "" + 2 + ""
                    + ", '" + nombres + "'"
                    + ", '" + apellidos + "'"
                    + ", '" + direccion + "'"
                    + ", '" + telefono + "'"
                    + ", '" + usuario + "'"
                    + ", '" + clave + "'"
                    + ")");
            setSuccessMessage("Usuario creado exitosamente.");

           }
        } catch (SQLException ex) {
            System.out.println("Error en registro de usuarios " + ex.getMessage());
        } finally {
            session.setAttribute("successMessage", getSuccessMessage());
            session.setAttribute("errorMessage", getErrorMessage());
            setSuccessMessage("");
            setErrorMessage("");
            response.sendRedirect(irA+".jsp");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}